<?php

class Overdue extends M_Controller
{

    // crontab 调用, 将订单关闭
    public function orderOverdue(): bool
    {
        $overDueSql = $this->db->select('id,oid,cid,type')->where([
            'end_time <' => time(),
            'status' => 0
        ])->order_by('id asc')
            ->limit(50)->get('1_order_queue');
        while ($row = $overDueSql->unbuffered_row()) {
            if ($row->oid > 0) {
                if (in_array($row->type, ['box', 'favorite'])) {
                    $this->models('favorite')->operateOrder($row->oid, ['order_status' => 9]);
                } elseif ($row->type == 'market') {
                    ### 更新市场表
                    $this->models('market')->operateOrder($row->oid, ['order_status' => 9]);
                    ### 取消市场内的锁状态
                    $this->models('market')->operateLock($row->cid, 0);
                }
                ### 更新队列状态
                $this->models('favorite')->upQueue($row->id);
                ### 删除1小时内无效队列
                $this->models('favorite')->delOverQueue(strtotime("-1 hours"));
            }
        }
        return true;
    }
}